﻿function message(msg) {
		$.messager.show({
			title : '提示',
			msg : msg,
			timeout : 3000,
			showType : 'slide'
		});
	}
	
	function disconnect()
	{
		if(isComOpen==true){
			var iRet;
			iRet = obj.exit(icdev);
			if(iRet)
			{
				message("断开连接失败");
			}
			else
			{
				//message("成功断开连接");
				isComOpen=false; //设置串口为关闭状态
			}
		}
	}
	function getUserPwdFromManagerCard(){
	    var pwd = obj.getUserPwdFromManagerCard(icdev);
        if(pwd == ""){
        	message("操作员卡读取失败");
        	return "";
        }else{
        	message("操作员卡读取成功");
        	return pwd;
        }
	}
	
	function connect() {
		var iRet;
		try {
			if (isComOpen == false) //如果读卡器的串口还没有打开
			{
				iRet = obj.initialcom(100, 115200); //连接读写器，100为USB口， 115200为波特率
				if (iRet) {
					icdev = iRet;
					//hdev = obj.beep(iRet, 10); //使读写器设备蜂鸣
					message("连接读卡器成功！");
					isComOpen = true; //读卡器设为打开状态
				} else {
					message("读卡器连接失败！");
					isComOpen = false; //读卡器设为未打开状态
				}
			}
		} catch (e) {
			alert(e.message);
		}

		return;
	}
	
	// 寻卡
	function findCard()
	{
		if(!isComOpen) connect();
	     var strcard="";
	     strcard = obj.findcardHex(icdev,1);      //1为读多张卡模式
		if(strcard!="")
		{
			hasCard =true;
			return strcard;
		}
		else
		{
			message("寻卡失败");
			hasCard =false;        //设置为当前无卡状态
			return "";
		}
	}
	
	//装载密码
	function VerifyPin(section)
	{
		var verifyRet = false;
		verifyRet = obj.loadkey(icdev,0,section,$(".p1").text());      //装载密码,0为装载密码A，2为扇区，ffffffffffff为密码
		if(verifyRet)
		{
			message("装载密码失败！");
		}
		else
		{
			message("装载密码成功");
		}
		return;
	}
	function findNormalCard(){
		var data = readCard(0,2);
		if(data){
			data=data.substring(0,8);
			data = "00000000"+parseInt(data,16);
			return data.substring(data.length-8);
		}
	}
	function findMoneyCard(){
		var data = readCard(4,17);
		if(data){
			data=data.substring(16,24);
		}
		data = data-0;
		data = data/100;
		return data;
	}
	function writeMoneyCard(price){
		price=price*100;
		price = "00000000"+price;
		price = price.substring(price.length-8);
		var data = "00000000000000000000090000000000";//32
		data = "0000000000000000"+price+"00000000";
		return writeCard(4,17,data);
	}

	//写卡
	function writeCard(section,chunk,data)
	{
		connect();
        var strcard="";
        var  iRet=0;
		strcard=obj.findcardHex(icdev,1);//多卡模式(1)寻卡
		if(strcard!="")
		{
			message("寻卡成功");
		}
		else
		{
			message("寻卡失败");
			return false;
		}
		
		 iRet = obj.authentication(icdev,0,section);//验证第2扇区        0为模式，2为第2扇区
		if(iRet)
		{
			message("密码校验失败");
			return false;
		}
		else
		{
			message("密码校验成功");
		}
		iRet = obj.write(icdev,chunk,data);   //8为写第八块的数据
		//iRet = obj.directWrite(icdev,8,"深圳峰华科技公司");//直接写数据，汉字为8个，纯字符串为16个
//		iRet=obj.write(icdev,9,"11111111111111111111111111111111");
		if(iRet == 0)
		{
			message("写卡成功");
		}
		else
		{
			message("写卡失败");
			return false;
		}
		return true;

	}
	function readCard(section,chunk){
		connect();
		VerifyPin(section);
		var iRet;
		var  data=" ";

		if(icdev==0)
		{
			message("读卡器错误");
			return;
		}
		var strcard="";
		//hdev=obj.loadkey(icdev,0,2,"ffffffffffff");
		strcard=obj.findcardHex(icdev,1);//多卡模式(1)寻卡
		
		iRet = obj.authentication(icdev,0,section);    //认证密码      0为模式，2为第2扇区
		if(iRet)
		{
			message("密码校验失败");
		}
		else
		{
			message("密码校验成功");
		}

		data =obj.read(icdev,chunk);        //8为读第八块的数据
		//data =obj.directRead(icdev,8);        //直接读块中数据，可能不可见
		
		if(data != "")
		{
			message("读卡成功");
			return data;
		}
		else
		{
			message("读卡失败");
		}
		return;
	} 
	$(window).bind('beforeunload',function(){
		disconnect();
	});
	var nRead = 0; //同一张卡循环读的次数
	var hasCard = false;
	var isComOpen = false;
	var icdev = 0;